package com.example.jsoupdemo.util;

import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class JsoupUtil {

    public static void crawText() throws IOException {
        long startTime = System.currentTimeMillis();
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        for (int i=1;i<2;i++){
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    //获取文本
                    Document document = DocumentUtil.againGetDocument("http://www.nanming.gov.cn/");
                    System.out.println(document.html());
                    String selector = "div";//定义选择器
                    Elements elements = document.select(selector);//抓取所有div标签的内容
                    System.out.println(elements.html());
                }
            });

        }
        //停止所有线程
        executorService.shutdown();
        while(true){
            if(executorService.isTerminated()){
                long endTime = System.currentTimeMillis();
                System.out.println((endTime-startTime)/1000F);
                break;
            }
        }

//        //创建连接
//        Connection connect = Jsoup.connect(listUrl);
//        //获取文本
//        Document document = connect.get();
//        //System.out.println(document.html());
//        String selector = "a";//定义选择器
//        Elements elements = document.select(selector);//抓取所有div标签的内容
//        System.out.println(elements.get(0).absUrl("href"));//获取地址
//        for (Element element:elements) {
//            Elements elementsl = element.select("a");
//            System.out.println(elementsl.html());
//        }

    }
    public static void main(String[] args) throws IOException {
        crawText();
    }
}
